/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.chlitina.store.modules.inventory.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.chlitina.store.common.config.Global;
import com.chlitina.store.common.mapper.JsonMapper;
import com.chlitina.store.common.persistence.Page;
import com.chlitina.store.common.web.BaseController;
import com.chlitina.store.modules.base.entity.StoreProduct;
import com.chlitina.store.modules.inventory.entity.StoreInventoryCheck;
import com.chlitina.store.modules.inventory.entity.StoreInventoryCheckDetail;
import com.chlitina.store.modules.inventory.service.StoreInventoryCheckDetailService;
import com.chlitina.store.modules.inventory.service.StoreInventoryCheckService;

/**
 * 库存盘点明细Controller
 * @author lvbo
 * @version 2013-11-18
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/inventory/storeInventoryCheckDetail")
public class StoreInventoryCheckDetailController extends BaseController {

	@Autowired
	private StoreInventoryCheckDetailService storeInventoryCheckDetailService;
	@Autowired
	private StoreInventoryCheckService storeInventoryCheckService;
	
	@ModelAttribute
	public StoreInventoryCheckDetail get(@RequestParam(required=false) Long id) {
		if (id != null){
			return storeInventoryCheckDetailService.get(id);
		}else{
			return new StoreInventoryCheckDetail();
		}
	}
	
	@RequestMapping(value = "list")
	public String list(StoreInventoryCheckDetail storeInventoryCheckDetail, HttpServletRequest request, HttpServletResponse response, Model model) {
		String inventoryCheckId = request.getParameter("sic_id");

        Page<StoreInventoryCheckDetail> page = storeInventoryCheckDetailService.find(new Page<StoreInventoryCheckDetail>(request, response), storeInventoryCheckDetail, inventoryCheckId); 
        StoreInventoryCheck inventoryCheck = storeInventoryCheckService.get(Long.parseLong(inventoryCheckId));
        model.addAttribute("inventoryCheck", inventoryCheck);
        model.addAttribute("page", page);
		return "modules/inventory/storeInventoryCheckDetail";
	}

	@RequestMapping(value = "form")
	public String form(StoreInventoryCheckDetail storeInventoryCheckDetail, HttpServletRequest request, Model model) {
		model.addAttribute("storeInventoryCheckDetail", storeInventoryCheckDetail);
		return "modules/inventory/storeInventoryCheckDetailForm";
	}

	@RequestMapping(value = "save")
	public String save(StoreInventoryCheckDetail storeInventoryCheckDetail, Model model, RedirectAttributes redirectAttributes) {
		float lossMoney = getLossMoney(storeInventoryCheckDetail);
		storeInventoryCheckDetail.setLossMoney(lossMoney);
		if (!beanValidator(model, storeInventoryCheckDetail)){
			return form(storeInventoryCheckDetail, null, model);
		}
		storeInventoryCheckDetailService.save(storeInventoryCheckDetail);
		addMessage(redirectAttributes, "调整数量成功");
		return "redirect:"+Global.ADMIN_PATH+"/inventory/storeInventoryCheck/scopeList";
	}
	
	@RequestMapping(value = "delete")
	public String delete(Long id, HttpServletRequest request, RedirectAttributes redirectAttributes) {
		@SuppressWarnings("unchecked")
		List<StoreProduct> spList = (List<StoreProduct>) request.getSession().getAttribute("checkScope");
		StoreInventoryCheckDetail checkDetail = storeInventoryCheckDetailService.get(id);
		StoreProduct sp = null;
		for (StoreProduct p : spList) {
			if (p.getId().equals(checkDetail.getStoreProduct().getId())) {
				sp = p;
			}
		}
		spList.remove(sp);
		request.getSession().setAttribute("checkScope", spList);
		storeInventoryCheckDetailService.delete(id);
		addMessage(redirectAttributes, "删除成功");
		return "redirect:"+Global.ADMIN_PATH+"/inventory/storeInventoryCheck/scopeList";
	}
	
	@ResponseBody
	@RequestMapping(value = "updateAmount")
	public String updateProductAmount(HttpServletRequest request) {
		String productId = request.getParameter("productId");
		String amount = request.getParameter("amount");
		StoreInventoryCheckDetail detail = storeInventoryCheckDetailService.get(Long.parseLong(productId));
		detail.setAfterAdjustAmount(Integer.parseInt(amount));
		float lossMoney = getLossMoney(detail);
		detail.setLossMoney(lossMoney);
		storeInventoryCheckDetailService.save(detail);
		return JsonMapper.getInstance().toJson(amount);
	}
	private float getLossMoney(StoreInventoryCheckDetail detail){
		int before = detail.getBeforeAdjustAmount();
		int after = detail.getAfterAdjustAmount();
		float retailPrice = detail.getRetailPrice();
		return  (before - after) * retailPrice;
	}
}
